package cn.bdqn.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import cn.bdqn.bean.Bill;
import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.BillDao;
import cn.bdqn.dao.impl.BillDaoImpl;
import cn.bdqn.service.BillService;

/**
 * BillService层的实现类
 * @author Administrator
 *
 */
public class BillServiceImpl implements BillService{
	private BillDao billDao = null;
	public BillServiceImpl(){
		billDao = new BillDaoImpl();
	}

	@Override
	public boolean addBill(Bill bill) {
		int row = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			row = billDao.addBill(con, bill);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		if(row > 0){
			System.out.println("add success");
			return true;
		}else{
			System.out.println("add failure");
			return false;
		}
	}

	@Override
	public boolean updateBill(Bill bill) {
		int row = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			row = billDao.updateBill(con, bill);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		if(row > 0){
			System.out.println("update success");
			return true;
		}else{
			System.out.println("update failure");
			return false;
		}
	}

	@Override
	public boolean deleteBill(Bill bill) {
		int row = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			row = billDao.deleteBill(con, bill);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		if(row > 0){
			System.out.println("delete success");
			return true;
		}else{
			System.out.println("delete failure");
			return false;
		}
	}

	@Override
	public int getCount(Bill bill) {
		int count = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			count = billDao.getCount(con, bill);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return count;
	}

	/**
	 * 根据查询条件获取账单列表
	 */
	@Override
	public List<Bill> getList(Bill bill, int pageIndex, int pageSize) {
		List <Bill> list =null;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			list = billDao.getList(con, bill, pageIndex, pageSize);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return list;
	}

	/**
	 * 根据id获取正个bill信息
	 */
	@Override
	public Bill getBillById(int id) {
		Bill bill = null;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			bill = billDao.getBillById(con, id);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return bill;
	}
	

}
